/***************************************************************************
 *   Copyright (C) 2009 by Docteur Pierre-Marie Desombre.   *
 *   pm.desombre@medsyn.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 3 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "bmi.h"
#include "bmiManager.h"
#include "config.h"

#include <externallinks/medintux/medintuxParser.h>
#include <externallinks/independant/nameindex.h>
#include <common/settings.h>
#include <common/constants.h>
#include <database/databasemanager.h>
#include <QtGui>
#include <QtCore>
#include <QtSql>

QString Age ;
QString Sexe ;
QString guid;
QMultiMap<int,QString> dateH,weightH,heightH;
QDate dateObs;
void myMessageOutput(QtMsgType type, const char *msg)
 {
     QFile file(qApp->applicationDirPath ()+"/errorLog.txt");
     file.open(QIODevice::ReadWrite|QIODevice::Append);
     QTextStream s(&file);
     switch (type) {
     case QtDebugMsg:
         s << "Debug: " << QString(msg) << "\n";
         break;
     case QtWarningMsg:
         s << "Warning: " << QString(msg) << "\n";
         break;
     case QtCriticalMsg:
         s << "Critical: " << QString(msg) << "\n";
         break;
     case QtFatalMsg:
         s << "Fatal: " << QString(msg) << "\n";
         abort();
     }

     file.close();
 }

int main(int argc , char *argv[]){
  qInstallMsgHandler(myMessageOutput);
  QApplication a(argc,argv);
  QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
  QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  //a.addLibraryPath(a.applicationDirPath()+"/lib_qt4");
  a.setApplicationName("bmi");
  QFile f(a.applicationDirPath ()+"/errorLog.txt");
  f.remove();
  QString callingProgram;
  settings set;
  if (set.isItFirstConfig())
  {
        BmiManager bmiManager;
        if (bmiManager.firsttimeMessage())
        {
            //set.setValue(Constants::FIRSTTIME,QVariant("no"));
            ConfigDialog  cw ;
            int ret = cw.exec();
            if (ret == QDialog::Accepted)
            {
                  cw.record();
                  bmiManager.configRecordAllRight();
                }
            else
            {
                bmiManager.exitOfApplicationMessage();
                a.quit();
                }
            }
       settings newset;
       callingProgram = newset.value(Constants::CALLING_PROGRAM).toString();
      }
   else
   {
       callingProgram = set.value(Constants::CALLING_PROGRAM).toString();
       }
  qWarning() << "main " << QString::number(__LINE__) << "callingProgram = " << callingProgram;
  QStandardItemModel *model = new QStandardItemModel;
  QHash<QString,QString> hashNumberOfWeightValues;
  if(callingProgram == "medintux"){
      medintuxParser *med = new medintuxParser ;
      model = med->inputOfDatas();
      }
  else if(callingProgram == "medintux-inside"){
      medintuxParser *med = new medintuxParser ;
      model = med->inputOfDatasInside();
      }
  else{
      DatabaseManager *surveyDatabase = new DatabaseManager();
      if(surveyDatabase->m_IsBmiConfigured == false){
      if(surveyDatabase->connectBmisurvey()) 
      {
          qWarning() << "bmiSurvey database is connected " 
                     << __FILE__ << " " << QString::number(__LINE__);
                     
          }
      }
      NameIndex ni ;
      if(ni.exec() == QDialog::Accepted){
          model = ni.getModelOfValues();
          }
          else{
              a.quit();
              return 0;
              }
      }
      bmi *b = new bmi(model); // Age sous forme 1961-02-06 = date of birth
           b-> calc();

  return a.exec();
}
